package com.dotndot.model;

import com.dotndot.controller.utils.*;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class MomPreference
{

    private static final String MOM_FILE_NAME = "momnmom";

    private Context mContext = null;
    private static MomPreference mMomPreference = null;
    private SharedPreferences mSharedPreference = null;
    private Editor mEditData = null;

    private static final String INITIAL_ENTRY = "INITIAL_ENTRY";
    private static final String AUTO_LOGIN_FLAG = "AUTO_LOGIN_FLAG";
    private static final String EMAIL = "EMAIL";
    private static final String PASSWORD = "PASSWORD";

    public static MomPreference getInstance()
    {
        if( mMomPreference == null )
        {
            mMomPreference = new MomPreference();
        }
        return mMomPreference;
    }

    public void initSharedPreferences( Context context )
    {
        mContext = context;
        mSharedPreference = context.getSharedPreferences(MOM_FILE_NAME, Activity.MODE_PRIVATE);
        mEditData = mSharedPreference.edit();
    }

    public boolean getInitialEntry()
    {
        if( mSharedPreference == null )
        {
            return false;
        }
        else
        {
            return mSharedPreference.getBoolean(INITIAL_ENTRY, false);
        }
    }

    public void setInitialEntry( boolean flag )
    {
        if( mEditData != null )
        {
            mEditData.putBoolean(INITIAL_ENTRY, flag);
            mEditData.commit();
        }
    }

    public boolean getAutoLoginFlag()
    {
        if( mSharedPreference == null )
        {
            return false;
        }
        else
        {
            return mSharedPreference.getBoolean(AUTO_LOGIN_FLAG, false);
        }
    }

    public void setAutoLoginFlag( boolean flag )
    {
        if( mEditData != null )
        {
            mEditData.putBoolean(AUTO_LOGIN_FLAG, flag);
            mEditData.commit();
        }
    }

    public String getEmail()
    {
        if( mSharedPreference == null )
        {
            return null;
        }
        else
        {
            String email = null;
            try
            {
                email = AES.decryptString(mContext, mSharedPreference.getString(EMAIL, null));
            }
            catch( Exception e )
            {
                e.printStackTrace();
            }
            return email;
        }
    }

    public void setEmail( String email )
    {
        if( mEditData != null )
        {
            String aesEmail = null;
            try
            {
                aesEmail = AES.encryptString(mContext, email);
                mEditData.putString(EMAIL, aesEmail);
                mEditData.commit();
            }
            catch( Exception e )
            {
                e.printStackTrace();
            }
        }
    }

    public String getPassword()
    {
        if( mSharedPreference == null )
        {
            return null;
        }
        else
        {
            String password = null;
            try
            {
                password = AES.decryptString(mContext, mSharedPreference.getString(PASSWORD, null));
            }
            catch( Exception e )
            {
                e.printStackTrace();
            }
            return password;
        }
    }

    public void setPassword( String password )
    {
        if( mEditData != null )
        {
            String aesPassword = null;
            try
            {
                aesPassword = AES.encryptString(mContext, password);
                mEditData.putString(PASSWORD, aesPassword);
                mEditData.commit();
            }
            catch( Exception e )
            {
                e.printStackTrace();
            }
        }
    }
}
